import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { CmtVenueVenueInfoApi } from '#/api/cmt/venue/info';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { getRangePickerDefaultProps } from '#/utils';
import { getBaseCommunityOptions } from '#/api/cmt/base/community';
import { getMerchantInfoOptions } from '#/api/cmt/base/merchantInfo';
import { getVenueTypeOptions } from '#/api/cmt/venue/type';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'communityId',
      label: '社区',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择社区',
        api: getBaseCommunityOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'merchantId',
      label: '商户',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择商户',
        api: getMerchantInfoOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'typeId',
      label: '阵地类型',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择阵地类型',
        api: getVenueTypeOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'name',
      label: '阵地名称',
      rules: 'required',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入阵地名称',
      },
    },
    {
      fieldName: 'coverImage',
      label: '封面图片',
      component: 'ImageUpload',
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'images',
      label: '阵地图片',
      component: 'ImageUpload',
      formItemClass: 'col-span-2',
      componentProps: {
        limit: 10,
      },
    },
    {
      fieldName: 'address',
      label: '地址',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入地址',
      },
    },
    {
      fieldName: 'longitude',
      label: '经度',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入经度',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'latitude',
      label: '纬度',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入纬度',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'area',
      label: '面积(平方米)',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入面积',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'capacity',
      label: '容纳人数',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入容纳人数',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'openTime',
      label: '开放时间',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入开放时间，如：周一至周五 9:00-18:00',
      },
    },
    {
      fieldName: 'facilities',
      label: '设施标签',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入设施标签(JSON数组)',
      },
    },
    {
      fieldName: 'description',
      label: '阵地简介',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 3,
        placeholder: '请输入阵地简介',
      },
    },
    {
      fieldName: 'contactPerson',
      label: '联系人',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入联系人',
      },
    },
    {
      fieldName: 'contactPhone',
      label: '联系电话',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入联系电话',
      },
    },
    {
      fieldName: 'venueStatus',
      label: '状态',
      component: 'RadioGroup',
      formItemClass: 'col-span-2',
      componentProps: {
        options: [
          { label: '停用', value: 0 },
          { label: '可预约', value: 1 },
          { label: '使用中', value: 2 },
          { label: '维护中', value: 3 },
        ],
      },
      defaultValue: 1,
    },
    {
      fieldName: 'isRecommend',
      label: '是否推荐',
      component: 'RadioGroup',
      formItemClass: 'col-span-2',
      componentProps: {
        options: [
          { label: '否', value: 0 },
          { label: '是', value: 1 },
        ],
      },
      defaultValue: 0,
    },
    {
      fieldName: 'sort',
      label: '排序',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入排序',
        controlsPosition: 'right',
        class: '!w-full',
      },
      defaultValue: 0,
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'communityId',
      label: '社区ID',
      component: 'InputNumber',
      componentProps: {
        clearable: true,
        placeholder: '请输入社区ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'merchantId',
      label: '商户ID',
      component: 'InputNumber',
      componentProps: {
        clearable: true,
        placeholder: '请输入商户ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'typeId',
      label: '阵地类型ID',
      component: 'InputNumber',
      componentProps: {
        clearable: true,
        placeholder: '请输入阵地类型ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'name',
      label: '阵地名称',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入阵地名称',
      },
    },
    {
      fieldName: 'venueStatus',
      label: '状态',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: [
          { label: '停用', value: 0 },
          { label: '可预约', value: 1 },
          { label: '使用中', value: 2 },
          { label: '维护中', value: 3 },
        ],
        placeholder: '请选择状态',
      },
    },
    {
      fieldName: 'isRecommend',
      label: '是否推荐',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: [
          { label: '否', value: 0 },
          { label: '是', value: 1 },
        ],
        placeholder: '请选择是否推荐',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<CmtVenueVenueInfoApi.VenueInfo>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: 'ID',
      minWidth: 120,
    },
    {
      field: 'name',
      title: '阵地名称',
      minWidth: 150,
    },
    {
      field: 'typeId',
      title: '阵地类型ID',
      minWidth: 120,
    },
    {
      field: 'coverImage',
      title: '封面图片',
      minWidth: 100,
      cellRender: {
        name: 'CellImage',
        props: { size: 60 },
      },
    },
    {
      field: 'address',
      title: '地址',
      minWidth: 200,
    },
    {
      field: 'area',
      title: '面积(㎡)',
      minWidth: 100,
    },
    {
      field: 'capacity',
      title: '容纳人数',
      minWidth: 100,
    },
    {
      field: 'openTime',
      title: '开放时间',
      minWidth: 150,
    },
    {
      field: 'contactPerson',
      title: '联系人',
      minWidth: 120,
    },
    {
      field: 'contactPhone',
      title: '联系电话',
      minWidth: 120,
    },
    {
      field: 'venueStatus',
      title: '状态',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: {
          options: [
            { label: '停用', value: 0, color: 'error' },
            { label: '可预约', value: 1, color: 'success' },
            { label: '使用中', value: 2, color: 'warning' },
            { label: '维护中', value: 3, color: 'default' },
          ],
        },
      },
    },
    {
      field: 'isRecommend',
      title: '推荐',
      minWidth: 80,
      cellRender: {
        name: 'CellDict',
        props: {
          options: [
            { label: '否', value: 0 },
            { label: '是', value: 1 },
          ],
        },
      },
    },
    {
      field: 'viewCount',
      title: '浏览次数',
      minWidth: 100,
    },
    {
      field: 'bookingCount',
      title: '预约次数',
      minWidth: 100,
    },
    {
      field: 'rating',
      title: '评分',
      minWidth: 100,
    },
    {
      field: 'sort',
      title: '排序',
      minWidth: 80,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
